import { Injectable } from '@nestjs/common';
import { Profile, } from '@prisma/client';
import { PrismaService } from 'src/common/prisma/prisma.service';
import { CreateProfileDto } from './interfaces/profiles.dto';
import { ErrorResponse } from 'src/common/types/common.type';
import { ERROR_USER_NOT_EXIST } from 'src/common/types/error-mapping';

@Injectable()
export class ProfilesService {
    constructor(private prisma: PrismaService) { }

    async findAll(): Promise<Profile[]> {
        return this.prisma.profile.findMany({
            include: {
                user: {
                    select: {
                        id: true,
                        username: true,
                        email: true,
                        createdAt: true,
                        updatedAt: true,
                    },
                },
            }
        });
    }

    async creaeteProfile(payload: CreateProfileDto): Promise<Profile | ErrorResponse> {
        // check user exist
        if (!(await this.prisma.user.findUnique({
            where: {
                id: payload.userId,
            }
        }))) {
            return ERROR_USER_NOT_EXIST;
        }

       return await this.prisma.profile.create({
           data: {
                userId: payload.userId,
                avatar: payload.avatar,
           }
       })
    }
}